/**
 * Created by sun on 2017/3/28.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');
    require('upload');
    var toastr =require('toastr');
    app.useModule("ui.table");
    app.useModule("ngFileUpload");
    var datepicker = require('datepicker');
    app.controller('assetsProjectCtrl', ['$scope','$http','$filter','Upload' ,'$rootScope',function ($scope, $http,$filter,Upload,$rootScope) {
        datepicker($scope);
        $scope.showOnline=false;
        $scope.ctrlScope=$scope;
        $scope.ctrlScope.itemsearch={};
        $scope.isEdit=false;
        $scope.add={};
        $scope.list={};
        $scope.upImgs={};
        $scope.isPicUpload=0;
        $scope.select2Options = {
            minimumResultsForSearch: Infinity //不出现搜索框
        };
        $scope.selectOptions = {
            allowClear: false,
            language : 'zh-CN'
        };
        $scope.orderParams = {
            url: "/admin/assetProduct/getPageList"
        };
        //获取登录帐号信息
        $http({
            method: 'POST',
            url: "/admin/sysuser/getUserType"
        }).success(function(data) {
            $scope.userType=data.userType;
        }).error(function(data) {
        });
        //全局查询
        $scope.search = function() {
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };
        //全局查询重置
        $scope.resetSearch=function(){
            $scope.orderParams.ajaxParam={};
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.reload();
        };
        //新增
        $scope.addModal=function(){
            $scope.checkSubmitFlg = false;//自动匹配，当表单被提交过一次后checkSubmitFlg将变为true,根据判断将无法进行提交。
            $scope.add={};
            $scope.isEdit=false;
            $scope.detailModal=!$scope.detailModal;
        };
        //修改
        $scope.modifyBtn=function(id){
            $scope.checkSubmitFlg = false;//自动匹配，当表单被提交过一次后checkSubmitFlg将变为true,根据判断将无法进行提交。
            $scope.detailModal=!$scope.detailModal;
            $scope.getOne(id);
            $scope.isEdit=true;
        };
        //查看单个
        $scope.getOne=function(id){
            $http({
                method: 'POST',
                url: "/admin/assetProduct/findById",
                data:{id:id}
            }).success(function(data) {
                $scope.add=data;
                $scope.list=data;
                $scope.add.interestTime1=Number(data.interestTime);
            }).error(function(data) {
            });
        };
        //提交与修改
        $scope.addBtn=function(){
            if ($scope.checkSubmitFlg == true) {
                return false; //当表单被提交过一次后checkSubmitFlg将变为true,根据判断将无法进行提交。
            }
            $scope.checkSubmitFlg = true;
            $scope.add.interestTime=$scope.add.interestTime1;
            $scope.add.interestTime=$filter('date')($scope.add.interestTime,'yyyy-MM-dd');
            var param=$scope.add;
            console.log(param);
            $http({
                method: 'POST',
                url: "/admin/assetProduct/save",
                data:param
            }).success(function(data) {
                if(data.result=='success'){
                    if( $scope.isEdit==true){
                        toastr.success('修改成功');
                    }else{
                        toastr.success('新增成功');
                    }
                    $scope.detailModal=!$scope.detailModal;
                    $scope.orderParams.reload();
                }else{
                        toastr.error(data.messageText);
                }
            }).error(function(data) {
            });
        };
        //获取公司下拉框
        $scope.getCompany=function(){
            $http({
                method: 'POST',
                url: "/admin/assetCompany/getList"
            }).success(function(data) {
                $scope.companyList=data;
            }).error(function(data) {
            });
        };
        $scope.getCompany();
        //获取借款人列表
        $scope.getAsset=function(){
            $http({
                method: 'POST',
                url: "/admin/sysuser/getAssetList"
            }).success(function(data) {
                $scope.assetList=data;
            }).error(function(data) {
            });
        };
        $scope.getAsset();
        //删除
        $scope.removeBtn=function(id){
            if(confirm("确认删除此条商品?")) {
                $http({
                    method: 'POST',
                    url: "/admin/assetProduct/delete",
                    data: {
                        id:id
                    }
                }).success(function(data) {
                    if(data.result=="success"){
                        toastr.success("删除成功");
                        $scope.orderParams.reload();
                    }else{
                        if(data.messageText){
                            toastr.error(data.messageText);
                        }else{
                            toastr.error("删除失败");
                        }
                        return;
                    }
                }).error(function(data) {});
            } else {
                return;
            }
        };
        //图片上传Modal
        $scope.upLoadImg= function (id) {
            $scope.isPicUpload=1;
            $scope.imgId=id;
            $scope.personalImgs=[];
            $scope.carImgs=[];
            $scope.procedureImgs=[];
            $scope.getImg(id);
          $scope.upLoadModal=!$scope.upLoadModal;
        };
        //图片编辑
        $scope.picEdit=function(id){
            $scope.isPicUpload=0;
            $scope.imgId=id;
            $scope.getImg(id);
            $scope.upLoadModal=!$scope.upLoadModal;
        };
        //图片单个查看
        $scope.openMaxPic= function (img) {
            $scope.showOnline = !$scope.showOnline;
            $scope.maxPic=img;
        };
        //关闭图片上传页面
        $scope.imgBtn=function(){
            $scope.upLoadModal=!$scope.upLoadModal;
        };
        //上传图片
        $scope.uploadPic =function(file,type) {
            for(i=0;i<file.length;i++){
                file.upload = Upload.upload({
                    url: '/itemFile',
                    headers: {
                        'optional-header': 'header-value'
                    },
                    data: {file: file[i]}
                });
                file.upload.then(function (response) {
                    $http({
                        method: 'POST',
                        url: "/admin/assetPicture/add",
                        data: {
                            productId:$scope.imgId,
                            url:response.data.url,
                            type:type
                        }
                    }).success(function(data) {
                        if(data.result=="success"){
                            toastr.success("上传成功");
                            $scope.getImg($scope.imgId);
                        }else{
                            toastr.error("上传失败");
                            return;
                        }
                    }).error(function(data) {});
                    //上传成功
                }, function (response) {
                    if (response.status > 0)
                        $scope.errorMsg = response.status + ': ' + response.data;
                }, function (evt) {
                    file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
                    vm.value=file.progress;

                });
                file.upload.xhr(function (xhr) {
                    if (xhr.upload) {

                    }
                });
            }
        };
        //删除图片
        $scope.imgRemove = function(index,id) {
            if(confirm("确认删除此图片?")) {
            $http({
                method: 'POST',
                url: "/admin/assetPicture/delete",
                data: {
                    id:id
                }
            }).success(function(data) {
                if(data.result=="success"){
                    toastr.success("删除成功");
                    $scope.getImg($scope.imgId);
                }else{
                    toastr.error("删除失败");
                    return;
                }
            }).error(function(data) {});
            }else{
                return;
            }
        };
        //获取图片
        $scope.getImg=function(id){
            $http({
                method: 'POST',
                url: "/admin/assetPicture/getList",
                data: {
                    productId:id
                }
            }).success(function(data) {
                $scope.personalImgs=[];
                $scope.carImgs=[];
                $scope.procedureImgs=[];
                $scope.riskImgs=[];
                $scope.skillImgs=[];
                for(var i=0;i<data.length;i++){
                    if(data[i].type==1){
                        $scope.personalImgs.push(data[i]);
                    }else if(data[i].type==2){
                        $scope.carImgs.push(data[i]);
                    }else if(data[i].type==3){
                        $scope.procedureImgs.push(data[i]);
                    }else if(data[i].type==4){
                        $scope.riskImgs.push(data[i]);
                    }else if(data[i].type==5){
                        $scope.skillImgs.push(data[i]);
                    }
                }
            }).error(function(data) {});
        };
        //产品审核页面
        $scope.auditBtn=function(id){
            $scope.audit={};
            $scope.riskImgs=[];
            $scope.skillImgs=[];
            $scope.auditModal=!$scope.auditModal;
            $scope.getOne(id);
            $scope.getImg(id);
            $scope.imgId=id;
        };
        //产品审核提交
        $scope.auditUpBtn=function(){
            $scope.audit.endTime=$scope.audit.endTime1;
            $scope.audit.endTime=$filter('date')($scope.audit.endTime,'yyyy-MM-dd');
            var param=$scope.audit;
                param.productId=$scope.imgId;
                param.interestTime=$scope.list.interestTime;
                param.debtServeFeeRate = $scope.list.debtServeFeeRate;
            console.info(param);

            $http({
                method: 'POST',
                url: "/admin/assetProduct/pushAssetPackage",
                data:param
            }).success(function(data) {
                if(data.result=="success"){
                    toastr.success("审核成功");
                    $scope.auditModal=!$scope.auditModal;
                    $scope.orderParams.reload();
                }else{
                    toastr.error(data.messageText);
                    return;
                }
            }).error(function(data) {});
        };

        /*-------------------------编辑图片---------------------------*/
        //退出
        $scope.cancel=function(){
         $scope.showOnline=!$scope.showOnline;
            $scope.getImg($scope.imgId);
        };
        //重置
        $scope.reset=function(maxPic){
            $scope.maxPic=maxPic;
        };



        //打码
        $scope.imgConfirm=function(x,y,endX,endY){
            $http({
                method: 'POST',
                url: "/admin/assetPicture/dealPicture",
                data:{id:$scope.maxPic.id,type:2,x:x,y:y,screenshotX:endX,screenshotY:endY}
            }).success(function(data) {
                if(data.result=="success"){
                    toastr.success("打码成功");
                }else{
                    toastr.error("打码失败");
                }
                $rootScope.$broadcast("imgChange",data);
            }).error(function(data) {
            });
        };
        //加水印
        $scope.shuiyin=function(){
            $http({
                method: 'POST',
                url: "/admin/assetPicture/dealPicture",
                data:{id:$scope.maxPic.id,type:1,x:0,y:0,screenshotX:0,screenshotY:0}
            }).success(function(data) {
                if(data.result=="success"){
                    toastr.success("加水印成功");
                }else{
                    toastr.error("加水印失败");
                }
                $rootScope.$broadcast("imgChange",data);
            }).error(function(data) {
            });
        };
        //确认
        $scope.confirm=function(){
            $scope.showOnline=!$scope.showOnline;
            $scope.getImg($scope.imgId);
        };
        //重置
        $scope.reset=function(){
            $http({
                method: 'POST',
                url: "/admin/assetPicture/reset",
                data:{id:$scope.maxPic.id}
            }).success(function(data) {
                if(data.result=="success"){
                    toastr.success("重置成功");
                    $rootScope.$broadcast("imgChange",data);
                }else{
                    toastr.error("重置失败");
                }
            }).error(function(data) {
            });
        };

        //判断是否新手标或推荐标
        $scope.fangfaming=function(type,value){
            if(type==1&&value==1){
                $scope.audit.itemIsrecommend=0;
            }else if(type==2&&value==1){
                $scope.audit.itemIsnew=0;
            }
        };


    }]);
});